package com.ruoyi.project.upload.controller;

import java.io.*;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.afterturn.easypoi.cache.manager.POICacheManager;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.upload.domain.UploadImages;
import com.ruoyi.project.upload.service.IUploadImagesService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传图片信息Controller
 *
 * @author fanzhihao
 * @date 2023-03-23
 */
@RestController
@RequestMapping("/upload/uploadImage")
public class UploadImagesController extends BaseController {
    @Autowired
    private IUploadImagesService uploadImagesService;

    /**
     * 查询上传图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:list')")
    @GetMapping("/list")
    public TableDataInfo list(UploadImages uploadImages) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!user.isAdmin()){
            uploadImages.setUserId(user.getUserId());
        }
        startPage();
        List<UploadImages> list = uploadImagesService.selectUploadImagesList(uploadImages);
        return getDataTable(list);
    }

    /**
     * 导出上传图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:export')")
    @Log(title = "上传图片信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UploadImages uploadImages) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!user.isAdmin()){
            uploadImages.setUserId(user.getUserId());
        }
        List<UploadImages> list = uploadImagesService.selectUploadImagesList(uploadImages);


        ExcelUtil<UploadImages> util = new ExcelUtil<UploadImages>(UploadImages.class);
        util.exportExcel(response, list, "支付截图");

    }


    /**
     * 导出上传图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:export')")
    @Log(title = "上传图片信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, UploadImages uploadImages) throws IOException {

        InputStream tplInputStream = POICacheManager.getFile("template//chuchai.xlsx");
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!user.isAdmin()){
            uploadImages.setUserId(user.getUserId());
        }
        List<UploadImages> list = uploadImagesService.selectUploadImagesList(uploadImages);
        EasyExcel.write(response.getOutputStream()).withTemplate(tplInputStream).sheet(1).doFill(list);

    }


    /**
     * 获取上传图片信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(uploadImagesService.selectUploadImagesById(id));
    }

    /**
     * 新增上传图片信息
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:add')")
    @Log(title = "上传图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UploadImages uploadImages) {
        return toAjax(uploadImagesService.insertUploadImages(uploadImages));
    }

    /**
     * 修改上传图片信息
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:edit')")
    @Log(title = "上传图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UploadImages uploadImages) {
        return toAjax(uploadImagesService.updateUploadImages(uploadImages));
    }

    /**
     * 删除上传图片信息
     */
    @PreAuthorize("@ss.hasPermi('upload:uploadImage:remove')")
    @Log(title = "上传图片信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(uploadImagesService.deleteUploadImagesByIds(ids));
    }

    /**
     * 图片上传
     *
     * @param files
     * @return
     */
    @Log(title = "上传图片信息", businessType = BusinessType.INSERT)
    @PostMapping("/img")
    public AjaxResult uploadImages(List<MultipartFile> files) {
        return toAjax(uploadImagesService.uploadImages(files));
    }
}
